package ihm.panel.file;

import ihm.applet.KrakenApplet;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.hibernate.Session;
import org.hibernate.Transaction;

import tools.PasswordSecurity;

import bdd.standalone.bean.User;

//import bdd.internal.bean.User;

/**
 * Dialogue permettant de réinitiaiser son mot de passe.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class ChangePassword extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private KrakenApplet applet;
	private JPanel passwordPanel_jp, button_jp;
	private JTextField newPassword_jtf, confirmPassword_jtf;
	private JPanel messagePanel;
	private JLabel modify_jl, newPassword_jl, confirmPassword_jl;
	private JButton confirm_jb;
	
	/**
	 * Constructeur.
	 * @param applet
	 */
	public ChangePassword(KrakenApplet applet) {
		this.applet = applet;
		
		this.setLayout(new BorderLayout());
		
		this.initTextPanel();
		this.initMessagePanel();
		this.initButtonPanel();
		this.initActions();
		
		
		this.setTitle("Première Connexion");
		this.setLocationRelativeTo(null);
		this.pack();
		this.setResizable(false);
		this.pop();
	}
	
	/**
	 * Initialise le JDialog.
	 */
	private void initTextPanel() {
		this.passwordPanel_jp = new JPanel();
		this.passwordPanel_jp.setLayout(new GridLayout(2,2));
		//this.passwordPanel_jp.setBorder(BorderFactory.createEtchedBorder());

		this.newPassword_jl = new JLabel("Nouveau mot de passe : ");
		this.confirmPassword_jl = new JLabel("Confirmation mot de passe : ");
		
		this.newPassword_jtf = new JPasswordField();
		this.confirmPassword_jtf = new JPasswordField();
		
		this.passwordPanel_jp.add(this.newPassword_jl);
		this.passwordPanel_jp.add(this.newPassword_jtf);
		
		this.passwordPanel_jp.add(this.confirmPassword_jl);
		this.passwordPanel_jp.add(this.confirmPassword_jtf);
		
		this.add(passwordPanel_jp,BorderLayout.CENTER);
	}
	
	/**
	 * Initialise les boutons.
	 */
	private void initButtonPanel() {
		this.button_jp = new JPanel();
		this.confirm_jb = new JButton("Confirmer");
		this.button_jp.add(confirm_jb);
		this.add(button_jp,BorderLayout.SOUTH);
	}
	
	/**
	 * Initialise les actions.
	 */
	private void initActions(){
		this.confirm_jb.addActionListener(this);
	}
	
	/**
	 * Initialise les compossants de texte.
	 */
	private void initMessagePanel() {
		this.messagePanel = new JPanel();
		this.modify_jl= new JLabel("Vous effectuez votre première connexion.\n" +
				"Veuillez modifier votre mot de passe :");
		
		this.messagePanel.add(this.modify_jl);
		
		this.add(messagePanel, BorderLayout.NORTH);
	}

	/**
	 * Affiche le JDialog.
	 */
	public void pop() {
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(confirm_jb)) {
			if(this.newPassword_jtf.getText().equalsIgnoreCase(this.confirmPassword_jtf.getText())){
				Session session = applet.getSession();
				Transaction transaction = session.beginTransaction();
				
				//User user = applet.getUser();
				User user = (User) session.load(User.class, applet.getUser_ID());
				
				byte[] password = PasswordSecurity.sha256(this.confirmPassword_jtf.getText(), "");
				user.setUser_pwd(password);
				user.setUser_firstcon(false);
				//Debug
				//System.out.println(user);
				
				session.save(user);
				transaction.commit();

				//applet.setUser(user);
				applet.setUser_ID(user.getUser_id());
				
				session.close();
				
				
				this.setVisible(false);
			}
			else{
				JOptionPane.showMessageDialog(this, 
						"Les mots de passes insérés sont différents : veuillez les resaisir.", 
                        "Mots de passe différents", 
                        JOptionPane.ERROR_MESSAGE );
				this.setVisible(true);
			}
		}
	}
	
}
